package mdm.com.sap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sap.conn.jco.JCoContext;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoField;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.JCoStructure;
import com.sap.conn.jco.JCoTable;

import mdm.com.sap.model.Hu_Info;
import mdm.com.sap.model.Packing_station;
import mdm.com.sap.model.Return_Result;
import mdm.com.sap.model.Tnote_Hdr;
import mdm.com.sap.model.Tnote_Hu_TP;

public class Sap_Operation {

    private static final Logger LOGGER = LoggerFactory.getLogger(Sap_Operation.class);

    /**
     * 查询转移单 获取转移单详细信息
     * 
     * @param TNOTE_NUMBER
     *            转移单号
     */
    public static Tnote_Hdr GetTnote(String TNOTE_NUMBER) {
        LOGGER.info("转移单查询（转移单号）：" + TNOTE_NUMBER);
        Tnote_Hdr tnote = new Tnote_Hdr();
        try {
            // 如果TNOTE_NUMBER 长度大于8 则为HU号
            if (TNOTE_NUMBER.trim().length() > 8) {
                Hu_Info info = Mod_FG.ZBAPI_GET_DATA_BY_HU_yz(TNOTE_NUMBER.trim());
                if (info.getTnote_number() != null) {
                    TNOTE_NUMBER = info.getTnote_number();
                }
                LOGGER.info("转移单查询（HU号）：" + TNOTE_NUMBER);
            }
            
            JCoFunction function = RfcManager.getFunction("ZBAPI_FGWH_GET_TRANSFER_NOTE");
            JCoParameterList importParam = function.getImportParameterList();
            importParam.setValue("TNOTE_NUMBER", TNOTE_NUMBER.trim()); // 转移单号
            function.execute(RfcManager.getDestination());
            JCoStructure tnoteReturn = function.getExportParameterList().getStructure("RETURN");
            Return_Result res = SapUtil.GetResult(tnoteReturn);
            switch (res.getT()) {
                case "S":
                case "":
                    JCoStructure _TNOTE_HDR = function.getExportParameterList().getStructure("TNOTE");
                    tnote.setCreatedby(_TNOTE_HDR.getString("CREATEDBY"));
                    tnote.setCreationdate(_TNOTE_HDR.getString("CREATIONDATE"));
                    tnote.setLastchangedby(_TNOTE_HDR.getString("LASTCHANGEDBY"));
                    tnote.setLastchangeddate(_TNOTE_HDR.getString("LASTCHANGEDDATE"));
                    tnote.setOrdernumber(_TNOTE_HDR.getString("ORDERNUMBER"));
                    tnote.setPscode(_TNOTE_HDR.getString("PSCODE"));
                    tnote.setTnote_number(_TNOTE_HDR.getString("TNOTE_NUMBER"));
                    tnote.setTnote_status(_TNOTE_HDR.getString("TNOTE_STATUS"));
                    
                    tnote.setTotal_no_of_box("0");
                    tnote.setTotal_vemng("0");
                    if (!res.getT().equals("E")) {
                        // 取出HU
                        JCoTable _TNOTE_HU = function.getExportParameterList().getTable("TNOTE_HU");
                        List<String> _TargetEXDIVs = new ArrayList<String>();
                        for (int j = 0; j < _TNOTE_HU.getNumRows(); j++) {
                            _TNOTE_HU.setRow(j);
                            _TargetEXDIVs.add(_TNOTE_HU.getString("EXIDV"));
                        }
                        LOGGER.info("==== hu number _TargetEXDIVs  size===="+_TargetEXDIVs.size());
                        List<Hu_Info> list = Mod_FG.ZBAPI_GET_DATA_BY_HU(_TargetEXDIVs);
                        LOGGER.info("==== hu number select  list  size===="+list.size());
                        tnote.setList(list);
                        if(list!=null) {
                            for(Hu_Info info : list) {
                                tnote.setVbeln(info.getVbeln());
                                tnote.setPosnr(info.getPosnr());
                                tnote.setCustomer_po(info.getBstkd());
                                tnote.setCustomer_name(info.getName1());
                                tnote.setSap_no(info.getMatnr());
                                break;
                            }
                        }
                    }
                    break;
                default:
                    break;
            }
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return tnote;
    }
    
    /**
     * 查询生产车间列表
     * @return Packing_station  list集合
     */
    public static List<Packing_station> getPackStation(){
        List<Packing_station> list = new ArrayList<Packing_station>();
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_FGWH_GET_PACK_STATION");
            function.execute(RfcManager.getDestination());
            Return_Result res = SapUtil.GetResult(function.getExportParameterList().getStructure("RETURN"));
            switch (res.getT()) {
                case "":
                    JCoTable PACKING_STATION = function.getExportParameterList().getTable("PACKING_STATION");
                    for (int j = 0; j < PACKING_STATION.getNumRows(); j++) {
                        PACKING_STATION.setRow(j);
                        Packing_station ps=new Packing_station();
                        ps.setPscode(PACKING_STATION.getString("PSCODE"));
                        ps.setPsdesc(PACKING_STATION.getString("PSDESC"));
                        list.add(ps);
                    }
                    break;
                default:
                    break;
            }
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return list;
    }
    
    /**
     * 继原有转移单新增HU信息
     * @param hdr  转移单信息
     * @param AddedHU  新增HU号
     * @return
     */
    public static Return_Result AddTnote(Tnote_Hdr hdr,List<String> AddedHU) {
        Return_Result res = null;
        if(AddedHU.size() > 0) {
            try {
                JCoFunction function = RfcManager.getFunction("ZBAPI_FGWH_CHANGE_TNOTE");
                JCoStructure _TNOTE_HDR = function.getImportParameterList().getStructure("TNOTE_HDR");
                _TNOTE_HDR.setValue("ORDERNUMBER", hdr.getOrdernumber());
                _TNOTE_HDR.setValue("TNOTE_NUMBER", hdr.getTnote_number());
                _TNOTE_HDR.setValue("TNOTE_STATUS", hdr.getTnote_status());
                
                JCoTable _TNOTE_HU = function.getImportParameterList().getTable("TNOTE_HU");
                
                List<Hu_Info> list_hu_info =SapUtil.removeReiterationToInfo(Mod_FG.ZBAPI_GET_DATA_BY_HU(AddedHU));
                
                for(Hu_Info info : list_hu_info) {
                    _TNOTE_HU.appendRow();
                    LOGGER.info("====AddTnote====hu number="+info.getExidv());
                    _TNOTE_HU.setValue("EXIDV", SapUtil.ForNum(info.getExidv(),20));
                    _TNOTE_HU.setValue("AUFNR", info.getAufnr());
                }
//                for(String str : AddedHU) {
//                    _TNOTE_HU.appendRow();
//                    LOGGER.info("====AddTnote====hu number="+str);
//                    _TNOTE_HU.setValue("EXIDV", SapUtil.ForNum(str,20));
//                    _TNOTE_HU.setValue("AUFNR", "");
//                }
                function.execute(RfcManager.getDestination());
                res = SapUtil.GetResult(function.getExportParameterList().getStructure("RETURN"));
                switch (res.getT()) {
                    case "S":
                    case "":
                        res.setMESSAGE(hdr.getTnote_number());
                        break;
                    default:
                        break;
                }
            } catch (JCoException e) {
                e.printStackTrace();
            }
        }
        return res;
    }
    
    
    
    /**
     * 删除转移单中HU信息
     * @param hdr  转移单信息
     * @param AddedHU  需删除的HU号
     * @return
     */
    public static Return_Result DeleteTnote(Tnote_Hdr hdr,List<String> AddedHU) {
        Return_Result res = null;
        if(AddedHU.size() > 0) {
            try {
                JCoFunction function = RfcManager.getFunction("ZBAPI_FGWH_DELETE_TNOTE_ITEM");
                JCoStructure _TNOTE_HDR = function.getImportParameterList().getStructure("TNOTE_HDR");
                _TNOTE_HDR.setValue("ORDERNUMBER", hdr.getOrdernumber());
                _TNOTE_HDR.setValue("TNOTE_NUMBER", hdr.getTnote_number());
                _TNOTE_HDR.setValue("TNOTE_STATUS", hdr.getTnote_status());
                
                JCoTable _TNOTE_HU = function.getImportParameterList().getTable("TNOTE_HU");
                
                for(String str : AddedHU) {
                    _TNOTE_HU.appendRow();
                    LOGGER.info("====DeleteTnote====hu number="+str);
                    _TNOTE_HU.setValue("EXIDV", SapUtil.ForNum(str,20));
                }
                function.execute(RfcManager.getDestination());
                res = SapUtil.GetResult(function.getExportParameterList().getStructure("RETURN"));
                switch (res.getT()) {
                    case "S":
                    case "":
                        break;
                    default:
                        break;
                }
            } catch (JCoException e) {
                e.printStackTrace();
            }
        }
        return res;
    }

    
    /**
     * 创建新的转移单
     * @param hdr
     * @param AddedHU
     * @return
     */
    public static Return_Result CreateTnote(Tnote_Hdr hdr,List<String> tnote_Hu) {
        Return_Result res = null;
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_FGWH_CREATE_TNOTE");
            JCoStructure _TNOTE_HDR = function.getImportParameterList().getStructure("TNOTE_HDR");
            _TNOTE_HDR.setValue("PSCODE", hdr.getPscode());
            _TNOTE_HDR.setValue("CREATIONDATE", SapUtil.formatToday());
            _TNOTE_HDR.setValue("ORDERNUMBER", hdr.getOrdernumber());
            _TNOTE_HDR.setValue("TNOTE_NUMBER", hdr.getTnote_number());
            _TNOTE_HDR.setValue("TNOTE_STATUS", hdr.getTnote_status());
            
            JCoTable _TNOTE_HU = function.getImportParameterList().getTable("TNOTE_HU");
            
            
            List<Hu_Info> list_hu_info =SapUtil.removeReiterationToInfo(Mod_FG.ZBAPI_GET_DATA_BY_HU(tnote_Hu));
            
            for(Hu_Info info : list_hu_info) {
                _TNOTE_HU.appendRow();
                LOGGER.info("====AddTnote====hu number="+info.getExidv());
                _TNOTE_HU.setValue("EXIDV", SapUtil.ForNum(info.getExidv(),20));
                _TNOTE_HU.setValue("AUFNR", info.getAufnr());
            }
//            for(Tnote_Hu hu : tnote_Hu) {
//                _TNOTE_HU.appendRow();
//                _TNOTE_HU.setValue("EXIDV", SapUtil.ForNum(hu.getExidv(),20));
//            }
            function.execute(RfcManager.getDestination());
            res = SapUtil.GetResult(function.getExportParameterList().getStructure("RETURN"));
            switch (res.getT()) {
                case "S":
                case "":
                    res.setMESSAGE(function.getExportParameterList().getValue("TNOTE_NUMBER").toString());
                    break;
                default:
                    break;
            }
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return res;
    }
    
    /**
     * 根据客人PO或销售单号或项目号 查询转移单号
     * @param PONo  客人PO
     * @param VBELN  销售单号
     * @param POSNO   项目号
     * @return
     */
    public static List<String> Cpo_Get_Tnote(String PONo,String VBELN,String POSNO){
        List<String> list = new ArrayList<String>();
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_FGWH_CPO_GET_TNOTE");
            JCoParameterList importParam = function.getImportParameterList();
            if(PONo!=null&&!PONo.equals("")) {
                importParam.setValue("CUSTOMER_PO", PONo);
            }
            if(VBELN!=null&&!VBELN.equals("")) {
                importParam.setValue("VBELN", VBELN);
            }
            if(POSNO!=null&&!POSNO.equals("")) {
                importParam.setValue("POSNR", POSNO);
            }
            function.execute(RfcManager.getDestination());
            Return_Result res = SapUtil.GetResult(function.getExportParameterList().getStructure("ZRETURN"));
            switch (res.getT()) {
                case "S":
                case "":
                    JCoTable RfcResult = function.getExportParameterList().getTable("TNOTE_NUMBER");
                    for (int j = 0; j < RfcResult.getNumRows(); j++) {
                        RfcResult.setRow(j);
                        list.add(RfcResult.getString(0));
                    }
                    break;
                default:
                    break;
            }
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return list;
    }
    
    /**
     * 保存收货信息
     * @param tnote_number  转移单号码
     * @param pLocation		     仓位
     * @param Tnote_Hu_TP		  
     * @return
     */
    public static Return_Result saveTransfer(String  tnote_number,String pLocation,List<Tnote_Hu_TP> listhu_tp) {
        Return_Result res = null;
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_FGWH_TNOTE_GOODS_MOV");
            function.getImportParameterList().setValue("TNOTE_NUMBER", tnote_number);
            function.getImportParameterList().setValue("LOCATION", pLocation);
            function.getImportParameterList().setValue("LOCK_TNOTE", "X");
            
            JCoTable BapiTable = function.getImportParameterList().getTable("TNOTE_HU_DETAIL");
            
            for(Tnote_Hu_TP hu_tp : listhu_tp) {
                BapiTable.appendRow();
                BapiTable.setValue("BSTKD", hu_tp.getBSTKD());
                BapiTable.setValue("CHARG", hu_tp.getCHARG());
                BapiTable.setValue("CO_J_3ASIZE", hu_tp.getCO_J_3ASIZE());
                BapiTable.setValue("CO_MATNR", hu_tp.getCO_MATNR());
                BapiTable.setValue("CO_POSNR", hu_tp.getCO_POSNR());
                BapiTable.setValue("CO_SCHEDULE_LINE", hu_tp.getCO_SCHEDULE_LINE());
                BapiTable.setValue("CREATEDBY", hu_tp.getCREATEDBY());
                BapiTable.setValue("CREATIONDATE", hu_tp.getCREATIONDATE());
                BapiTable.setValue("CUSTOMER_BARCODE", hu_tp.getCUSTOMER_BARCODE());
                BapiTable.setValue("CUSTOMER_BOX", hu_tp.getCUSTOMER_BOX());
                BapiTable.setValue("DIFF_GRID_VALUE", hu_tp.getDIFF_GRID_VALUE());
                BapiTable.setValue("DIFF_PART_NO", hu_tp.getDIFF_PART_NO());
                BapiTable.setValue("DIFF_UOM", hu_tp.getDIFF_UOM());
                BapiTable.setValue("EXIDV", hu_tp.getEXIDV());
                BapiTable.setValue("H_CREATEDBY", hu_tp.getH_CREATEDBY());
                BapiTable.setValue("H_CREATIONDATE", hu_tp.getH_CREATIONDATE());
                BapiTable.setValue("H_LASTCHANGEDBY", hu_tp.getH_LASTCHANGEDBY());
                BapiTable.setValue("H_LASTCHANGEDDATE", hu_tp.getH_LASTCHANGEDDATE());
                BapiTable.setValue("J_3ASIZE", hu_tp.getJ_3ASIZE());
                BapiTable.setValue("KDMAT", hu_tp.getKDMAT());
                BapiTable.setValue("KUNNR", hu_tp.getKUNNR());
                BapiTable.setValue("LASTCHANGEDBY", hu_tp.getLASTCHANGEDBY());
                BapiTable.setValue("LASTCHANGEDDATE", hu_tp.getLASTCHANGEDDATE());
                BapiTable.setValue("LGORT", hu_tp.getLGORT());
                BapiTable.setValue("MAKTX", hu_tp.getMAKTX());
                BapiTable.setValue("MATNR", hu_tp.getMATNR());
                BapiTable.setValue("NAME1", hu_tp.getNAME1());
                BapiTable.setValue("ORDERNUMBER", hu_tp.getORDERNUMBER());
                BapiTable.setValue("POSNR", hu_tp.getPOSNR());
                BapiTable.setValue("PSCODE", hu_tp.getPSCODE());
                BapiTable.setValue("RELEASE_DOCUMENT", hu_tp.getRELEASE_DOCUMENT());
                BapiTable.setValue("RM_BOXNO", hu_tp.getRM_BOXNO());
                BapiTable.setValue("SEL", hu_tp.getSEL());
                BapiTable.setValue("STATUS", hu_tp.getSTATUS());
                BapiTable.setValue("TNOTE_ISTATUS", hu_tp.getTNOTE_ISTATUS());
                BapiTable.setValue("TNOTE_NUMBER", hu_tp.getTNOTE_NUMBER());
                BapiTable.setValue("TNOTE_STATUS", hu_tp.getTNOTE_STATUS());
                BapiTable.setValue("V_MATNR", hu_tp.getV_MATNR());
                BapiTable.setValue("V_VRKME", hu_tp.getV_VRKME());
                BapiTable.setValue("VBELN", hu_tp.getVBELN());
                BapiTable.setValue("VEMEH", hu_tp.getVEMEH());
                BapiTable.setValue("VEMNG", hu_tp.getVEMNG());
                BapiTable.setValue("VENUM", hu_tp.getVENUM());
                BapiTable.setValue("VEPOS", hu_tp.getVEPOS());
                BapiTable.setValue("VKGRP", hu_tp.getVKGRP());
                BapiTable.setValue("WERKS", hu_tp.getWERKS());
                BapiTable.setValue("ZZMATNR", hu_tp.getZZMATNR());
                BapiTable.setValue("ZZPOSNR", hu_tp.getZZPOSNR());
                BapiTable.setValue("ZZVBELN", hu_tp.getZZVBELN());
                BapiTable.setValue("ZZVRKME", hu_tp.getZZVRKME());
            }
            
            function.execute(RfcManager.getDestination());
            
            res = SapUtil.GetResult(function.getExportParameterList().getStructure("RETURN"));
            
            LOGGER.info("==type==="+res.getT());
            LOGGER.info("==message==="+res.getMESSAGE());
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return res;
    }
    
    
    /**
     * 根据交货单 清单号码 查询出HU号码
     * @param pDNote  交货单号
     * @param pPackID   清单号码
     * @return
     */
    public static List<String> GetDn_Pck_List(String pDNote,String pPackID,String user_type) {
        LOGGER.info("==GetDn_Pck_List===：pDNote={},pPackID={}  [" + pDNote+"],["+pPackID+"]");
        List<String> list =  new ArrayList<String>();
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_GET_DN_PCK_LST");
            JCoParameterList importParam = function.getImportParameterList();
            importParam.setValue("I_VBELN", SapUtil.ForNum(pDNote, 10)); 
            importParam.setValue("I_BOLNR", pPackID);
            importParam.setValue("USER_TYPE", user_type);
            
            function.execute(RfcManager.getDestination());
            
            Return_Result res = SapUtil.GetResult(function.getTableParameterList().getTable("RETURN"));
            LOGGER.info("==type=="+res.getT());
            LOGGER.info("==message=="+res.getMESSAGE());
            if(res.getT()==null || res.getT().equals("")) {
                res.setT("S");
            }
            
            if(res.getT().equals("S")) {
                JCoTable T_OUTPUT = function.getTableParameterList().getTable("T_OUTPUT");
                for(int i=0;i<T_OUTPUT.getNumRows();i++) {
                    T_OUTPUT.setRow(i);
                    list.add(T_OUTPUT.getString("EXIDV"));
                }
            }
            
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return list;
    }
    
    
    /**
     * 交货单保存
     * @param pDNote  交货单号  vbeln
     * @param _TargetEXDIVs  HU号集合
     * @return
     */
    public static Return_Result Save_Pck_Link(String pDNote,List<String> _TargetEXDIVs) {
        LOGGER.info("==GetDn_Pck_List===：pDNote={},_TargetEXDIVs={}  [" + pDNote+"],["+_TargetEXDIVs+"]");
        Return_Result res = null;
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_DN_HU_LINK");
            JCoTable BapiTable_DN = function.getTableParameterList().getTable("T_DN");
            BapiTable_DN.appendRow();
            BapiTable_DN.setValue("VBELN", SapUtil.ForNum(pDNote, 10));
            
            JCoTable BapiTable_HU = function.getTableParameterList().getTable("T_HU");
            
            for(String str : _TargetEXDIVs) {
                BapiTable_HU.appendRow();
                BapiTable_HU.setValue("EXIDV", SapUtil.ForNum(str, 20));
            }
            
            function.execute(RfcManager.getDestination());
            
            Return_Result r = SapUtil.GetResult(function.getTableParameterList().getTable("ZRETURN"));
            
            LOGGER.info("==LINK type=="+r.getT());
            LOGGER.info("== LINK message=="+r.getMESSAGE());
            
            if(r.getT().equals("") || r.getT().equals("S")) {
                JCoFunction pgifunction = RfcManager.getFunction("ZBAPI_DN_PGI");
                pgifunction.getImportParameterList().setValue("I_VBELN",  SapUtil.ForNum(pDNote, 10));
                pgifunction.execute(RfcManager.getDestination());
                res = SapUtil.GetResult(pgifunction.getTableParameterList().getTable("ZRETURN"));
                
                LOGGER.info("==PGI type=="+res.getT());
                LOGGER.info("== PGI message=="+res.getMESSAGE());
            }else {
                res = r;
            }
            
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return res;
    }
    
    
    /**
     * 扫描条码收货
     * @param pHuNo    HU号 集合
     * @param listhuinfo  HU号基本信息
     * @param pLocation   仓位
     * @param user_type
     * @return 
     */
    public static Return_Result Save_fgwh_Goods_Mov(List<String> pHuNo,List<Hu_Info> listhuinfo,String pLocation,String user_type) {
        Return_Result res = null;
        try {
            
            JCoFunction function = RfcManager.getFunction("ZBAPI_FGWH_GET_ORDER_FOR_TP2");
            JCoTable TBL_EXIDV = function.getTableParameterList().getTable("EXIDV");
            JCoTable TBL_HU_ORDER = function.getTableParameterList().getTable("HU_ORDER");
            for(String HU_Number : pHuNo) {
                for(Hu_Info info : listhuinfo) {
                    System.out.println("info.getExidv()=="+info.getExidv()+"       HU_Number="+SapUtil.ForNum(HU_Number, 20));
                    if(info.getExidv().equals(SapUtil.ForNum(HU_Number, 20))) {
                        TBL_EXIDV.appendRow();
                        TBL_EXIDV.setValue("EXIDV", SapUtil.ForNum(HU_Number, 20));
                        TBL_EXIDV.setValue("VEPOS", info.getVepos());
                        TBL_EXIDV.setValue("SONUM", info.getSonum());
                        TBL_EXIDV.setValue("AUFNR", info.getAufnr());
                        
                        TBL_HU_ORDER.appendRow();
                        TBL_HU_ORDER.setValue("EXIDV", SapUtil.ForNum(HU_Number, 20));
                        TBL_HU_ORDER.setValue("VEPOS", info.getVepos());
                        TBL_HU_ORDER.setValue("SONUM", info.getSonum());
                        TBL_HU_ORDER.setValue("AUFNR", info.getAufnr());
                    }
                }
//                if(HU_ORDER_DETAIL == null) {
//                    HU_ORDER_DETAIL = _HU_ORDER_DETAIL;
//                }else {
//                    for(int i=0;i<_HU_ORDER_DETAIL.getNumRows();i++) {
//                        _HU_ORDER_DETAIL.setRow(i);
////                        HU_ORDER_DETAIL.appendRow();
//                        HU_ORDER_DETAIL.setValue(HU_ORDER_DETAIL.getNumRows()+1, _HU_ORDER_DETAIL);;
//                        
//                    }
//                }
            }
            
            function.execute(RfcManager.getDestination());
            
            JCoTable _HU_ORDER_DETAIL = function.getTableParameterList().getTable("HU_ORDER_DETAIL");
            
            JCoFunction BapiFunction = RfcManager.getFunction("ZBAPI_FGWH_HU_GOODS_MOV");
            BapiFunction.getImportParameterList().setValue("LOCATION", pLocation);
            BapiFunction.getImportParameterList().setValue("USER_TYPE", user_type);
            
            JCoTable HU_TBL = BapiFunction.getImportParameterList().getTable("HU");
            for(String HU_Number : pHuNo) {
                HU_TBL.appendRow();
                HU_TBL.setValue("EXIDV", SapUtil.ForNum(HU_Number, 20));
            }
           
            BapiFunction.getImportParameterList().setValue("HU_DETAIL", _HU_ORDER_DETAIL);
            
            BapiFunction.execute(RfcManager.getDestination());
            
            res = SapUtil.GetResult(BapiFunction.getExportParameterList().getStructure("RETURN"));
            LOGGER.info("=========type======"+res.getT());
            LOGGER.info("=========message======"+res.getMESSAGE());
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return res;
    }
    
    
    
    /**
     * 取消收货
     * @param hulist
     * @param user_type
     * @return
     */
    public static Return_Result ZBAPI_CANCEL_GR_BY_HU(List<String> hulist,String user_type) {
        Return_Result res = null;
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_CANCEL_GR_BY_HU");
            
            function.getImportParameterList().setValue("USER_TYPE", user_type);
            
            JCoTable I_HU_NO = function.getImportParameterList().getTable("I_HU_NO");
            
            for(String str : hulist) {
                I_HU_NO.appendRow();
                I_HU_NO.setValue("SIGN", "I");
                I_HU_NO.setValue("OPTION", "EQ");
                I_HU_NO.setValue("LOW", SapUtil.ForNum(str, 20));
                I_HU_NO.setValue("HIGH", SapUtil.ForNum(str, 20));
            }
            function.execute(RfcManager.getDestination());
            
            res = SapUtil.GetResult(function.getTableParameterList().getTable("ZRETURN"));
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return res;
    }
    
    
    
    /**
     * 转仓
     * @param hulist   HU号list集合
     * @param pLoc	     仓位
     * @return
     */
    public static Return_Result ZBAPI_LOC_TX_BY_HU_NEW(List<String> hulist,String pLoc,String user_type) {
        Return_Result res = null;
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_LOC_TX_BY_HU");
            
            
            function.getImportParameterList().setValue("LGORT", pLoc);
            LOGGER.info("rool_over--------user_type="+user_type);
            function.getImportParameterList().setValue("USER_TYPE", user_type);
            
            JCoTable I_HU_NO = function.getImportParameterList().getTable("I_HU_NO");
            for(String str : hulist) {
                I_HU_NO.appendRow();
                I_HU_NO.setValue("SIGN", "I");
                I_HU_NO.setValue("OPTION", "EQ");
                I_HU_NO.setValue("LOW", SapUtil.ForNum(str, 20));
                I_HU_NO.setValue("HIGH", SapUtil.ForNum(str, 20));
            }
            function.execute(RfcManager.getDestination());
            
            res = SapUtil.GetResult(function.getTableParameterList().getTable("ZRETURN"));
            
            LOGGER.info("==type=="+res.getT());
            LOGGER.info("==message=="+res.getMESSAGE());
            
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return res;
    }
    
    
    /**
     * 更新重量
     * @param pHUKey  HU号
     * @param pWeight  新毛重
     * @param pNetWeight 新净重
     * @return
     */
    public static Return_Result BAPI_HU_CHANGE_HEADER_OLD(String pHUKey,String pWeight,String pNetWeight) {
        Return_Result res = null;
        try {
//            Map<String, Object> map = Sap_Operation.BAPI_HU_GETLIST(pHUKey);
            JCoFunction BapiTransactionCommit = RfcManager.getFunction("BAPI_TRANSACTION_COMMIT");
            JCoFunction function = RfcManager.getFunction("BAPI_HU_CHANGE_HEADER");
            
            function.getImportParameterList().setValue("HUKEY", SapUtil.ForNum(pHUKey, 20));
            
            JCoStructure _tmp = function.getImportParameterList().getStructure("HUCHANGED");
            
            _tmp.setValue("LOAD_WGHT", pNetWeight);
            _tmp.setValue("TOTAL_WGHT", pWeight);
            
            function.getImportParameterList().setValue("HUCHANGED", _tmp);
            
            JCoContext.begin(RfcManager.getDestination());
            function.execute(RfcManager.getDestination());
            BapiTransactionCommit.execute(RfcManager.getDestination());
            
            res = SapUtil.GetResult(function.getTableParameterList().getTable("RETURN"));
          
            JCoContext.end(RfcManager.getDestination());
            
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return res;
    }
    
    
    /**
     * 装箱单查询
     * @param pPackingID
     * @param user_type
     * @return
     */
    public static List<Hu_Info> ZBAPI_GET_HU_BY_PACKING(String pPackingID,String user_type){
        List<Hu_Info> list = new ArrayList<Hu_Info>();
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_GET_HU_BY_PACKING");
            function.getImportParameterList().setValue("ZPL_ID", pPackingID);
            function.getImportParameterList().setValue("USER_TYPE", user_type);
            
            function.execute(RfcManager.getDestination());
            
            Return_Result res = SapUtil.GetResult(function.getExportParameterList().getStructure("RETURN"));
            
            System.out.println(res.getT());
            System.out.println(res.getMESSAGE());
            switch(res.getT()) {
                case "E":
                    break;
                default:
                    JCoTable OUT_T_OUTPUT = function.getTableParameterList().getTable("HU_LIST");
                    for(int i=0;i<OUT_T_OUTPUT.getNumRows();i++) {
                        OUT_T_OUTPUT.setRow(i);
                        Hu_Info info = new Hu_Info();
                        info.setAufnr(OUT_T_OUTPUT.getString("AUFNR"));
                        info.setBrgew(OUT_T_OUTPUT.getString("BRGEW"));
                        info.setBstkd(OUT_T_OUTPUT.getString("BSTKD"));
                        info.setCharg(OUT_T_OUTPUT.getString("CHARG"));
                        info.setExidv(OUT_T_OUTPUT.getString("EXIDV"));
                        info.setExidv2(OUT_T_OUTPUT.getString("EXIDV2"));
                        info.setIsreturn(OUT_T_OUTPUT.getString("ISRETURN"));
                        info.setJ_3asize(OUT_T_OUTPUT.getString("J_3ASIZE"));
                        info.setKains(OUT_T_OUTPUT.getString("KAINS"));
                        info.setKalab(OUT_T_OUTPUT.getString("KALAB"));
                        info.setKdmat(OUT_T_OUTPUT.getString("KDMAT"));
                        info.setKunnr(OUT_T_OUTPUT.getString("KUNNR"));
                        info.setLgort(OUT_T_OUTPUT.getString("LGORT"));
                        info.setMaktx(OUT_T_OUTPUT.getString("MAKTX"));
                        info.setMatnr(OUT_T_OUTPUT.getString("MATNR"));
                        info.setMeins(OUT_T_OUTPUT.getString("MEINS"));
                        info.setName1(OUT_T_OUTPUT.getString("NAME1"));
                        info.setNtgew(OUT_T_OUTPUT.getString("NTGEW"));
                        info.setPosnr(OUT_T_OUTPUT.getString("POSNR"));
                        info.setSonum(OUT_T_OUTPUT.getString("SONUM"));
                        info.setStatus(OUT_T_OUTPUT.getString("STATUS"));
                        info.setTnote_number(OUT_T_OUTPUT.getString("TNOTE_NUMBER"));
                        info.setVbeln(OUT_T_OUTPUT.getString("VBELN"));
                        info.setVemng(OUT_T_OUTPUT.getString("VEMNG"));
                        info.setVpobjkey(OUT_T_OUTPUT.getString("VPOBJKEY"));
                        info.setWerks(OUT_T_OUTPUT.getString("WERKS"));
                        switch (OUT_T_OUTPUT.getString("MATCH").toUpperCase())
                        {
                            case "X":
                                info.setMatch("X");
                                break;
                            default:
                                info.setMatch("A");
                                break;
                        }
                        list.add(info);
                    }
            }
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return list;
    }
    
    
    
    /**
     * 装箱单验证HU
     * @param pPackingID  装箱清单号
     * @param hu_no	  HU号
     * @param user_type
     * @return
     */
    public static Return_Result ZBAPI_MTH_PCK_LST_CTN_BY_DATA(String pPackingID,String hu_no,String user_type) {
        Return_Result res = null;
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_MTH_PCK_LST_CTN_BY_DATA");
            function.getImportParameterList().setValue("ZPL_ID", pPackingID);
            function.getImportParameterList().setValue("I_HU_NO", SapUtil.ForNum(hu_no, 20));
            function.getImportParameterList().setValue("USER_TYPE", user_type);
            
            function.execute(RfcManager.getDestination());
            
            res = SapUtil.GetResult(function.getExportParameterList().getStructure("RETURN"));
            
            System.out.println("type===="+res.getT());
            System.out.println("message==="+res.getMESSAGE());
            switch(res.getT()) {
                case "S":
                    res.setNUM(hu_no);
                    res.setMESSAGE(function.getExportParameterList().getString("EXIDV2"));
                    break;
                case "":
                    res.setNUM(hu_no);
                    res.setMESSAGE(function.getExportParameterList().getString("EXIDV2"));
                    break;
                default:
                    break;
            }
            
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return res;
    }
    /**
     * 
     * @param huNo  hu号
     * @return
     */
    public static Map<String, Object> BAPI_HU_GETLIST(String huNo){
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            JCoFunction function = RfcManager.getFunction("BAPI_HU_GETLIST");
            function.getImportParameterList().setValue("NOTEXT", "");
            function.getImportParameterList().setValue("ONLYKEYS", "");
            
            JCoTable HUNUMBERS = function.getTableParameterList().getTable("HUNUMBERS");
            HUNUMBERS.appendRow();
            HUNUMBERS.setValue("HU_EXID", SapUtil.ForNum(huNo, 20));
            
            function.execute(RfcManager.getDestination());
            
            JCoTable _res = function.getTableParameterList().getTable("HUHEADER");
            
            Iterator<JCoField> jco = _res.iterator();
            while(jco.hasNext()) {
                JCoField next = jco.next();
                if(next != null) {
                    map.put(next.getName(), next.getValue());
                }
            }
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return map;
    }
}
